import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store/index'
import axios from '@/api/axios'

const originalPush = Router.prototype.push
Router.prototype.push = function push(location, resolve, reject) {
  if (resolve || reject) return originalPush.call(this, location, resolve, reject)
  return originalPush.call(this, location).catch((e) => { })
}

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView')
    },
    {
      path: '/home',
      // 在vue中@表示src目录
      component: () => import('@/views/HomeView.vue'),

      // 二级路由
      children: [
        {
          path: '/',
          redirect: '/home/correct'
        },
        {
          path: 'assignmentManage',
          name: "assignmentManage",
          component: () => import('@/components/assignmentManage/index'),
          meta: {
            active: "/home/assignmentManage" // 当前首页处于激活状态
          }
        },
        {
          path: 'edit/:id(\\d+)',
          component: () => import('@/components/editAssignment/index'),
          name: 'EditAssignment',
          meta: {
            active: "/home/assignmentManage" // 当前首页处于激活状态
          },
          hidden: true
        },
        {
          path: 'cabbage',
          name: 'Cabbage',
          component: () => import('@/components/corrent/Cabbage'),
          meta: {
            active: 'cabbage' // 当前首页处于激活状态
          }
        },
        {
          path: 'correct',
          name: 'Correct',
          component: () => import('@/components/corrent/Corrent'),
          meta: {
            active: '/home/correct' // 当前首页处于激活状态
          }
        },
        {
          path: 'tcorrent',
          name: 'Tcorrent',
          component: () => import('@/components/corrent/Tcorrent'),
          meta: {
            active: '/home/tcorrent' // 当前首页处于激活状态
          }
        },
        // {
        //   path: 'tommato',
        //   name: 'Tommato',
        //   component: () => import('@/components/corrent/Tommato'),
        //   meta: {
        //     active: 'home' // 当前首页处于激活状态
        //   }
        // },
        // 我的课程
        {
          path: 'teachercourse',
          name: 'TeacherCourse',
          component: () => import('@/components/mycourse/TeacherCourse'),
          meta: {
            active: '/home/teachercourse' // 当前首页处于激活状态
          }
        },
        {
          path: 'studentcourse',
          name: 'StudentCourse',
          component: () => import('@/components/mycourse/StudentCourse'),
          meta: {
            active: '/home/studentcourse' // 当前首页处于激活状态
          }
        },
        {
          path: 'detail',
          name: 'Detail',
          component: () => import('@/components/mycourse/CourseDetail'),
          meta: {
            active: 'detail' // 当前首页处于激活状态
          }
        },
        {
          path: 'addcourses',
          name: 'AddCourses',
          component: () => import('@/components/mycourse/AddCourses'),
          meta: {
            active: 'addcourses' // 当前首页处于激活状态
          }
        },
        // 个人中心
        {
          path: 'studentcentre',
          name: 'Personal',
          component: () => import('@/components/personal/StudentPersonal'),
          meta: {
            active: "/home/studentcentre" // 当前首页处于激活状态
          }
        },
        {
          path: 'teachercentre',
          name: 'Personal',
          component: () => import('@/components/personal/TeacherPersonal'),
          meta: {
            active: "/home/teachercentre" // 当前首页处于激活状态
          }
        },
        // 评价
        // {
        //   path: 'evaluate',
        //   name: 'Evaluate',
        //   component: () => import('@/components/evaluate/Evaluate'),
        //   meta: {
        //     active: 'home' // 当前首页处于激活状态
        //   }
        // },
        // 仪表盘
        // {
        //   path: 'panel',
        //   name: 'Panel',
        //   component: () => import('@/components/panel/Panel'),
        //   meta: {
        //     active: 'home' // 当前首页处于激活状态
        //   }
        // },
        // 通知
        {
          path: 'message',
          name: 'Panel',
          component: () => import('@/components/message/Message'),
          meta: {
            active: "/home/message" // 当前首页处于激活状态
          }
        },
        // 提交
        {
          path: 'publishjob',
          name: 'PublishJob',
          component: () => import('@/components/publishAssignment/index'),
          meta: {
            active: "/home/publishjob" // 当前首页处于激活状态
          }
        },
        {
          path: "publishnew",
          name: "PublishNew",
          component: () => import('@/components/publishjob/PublishNew'),
          meta: {
            active: "/home/publishjob" // 当前首页处于激活状态
          }
        },
        {
          path: "submitdetail",
          name: "SubmitDetail",
          component: () => import('@/components/submitjob/SubmitDetail'),
          meta: {
            active: "/home/submitjob" // 当前首页处于激活状态
          }
        },
        {
          path: 'submitjob',
          name: 'SubmitJob',
          component: () => import('@/components/submitjob/Submit'),
          meta: {
            active: "/home/submitjob" // 当前首页处于激活状态
          }
        },
        {
          path: 'studentCorrectStatus',
          name: 'studentCorrectStatus',
          component: () => import('@/components/studentCorrectStatus/index'),
          meta: {
            active: "/home/studentCorrectStatus" // 当前首页处于激活状态
          }
        },
        {
          path: "submitwork",
          name: "SubmitWork",
          component: () => import('@/components/submitjob/SubmitWork'),
          meta: {
            active: "/home/submitjob" // 当前首页处于激活状态
          }
        },
        {
          path: "lookwork",
          name: "LookWork",
          component: () => import('@/components/submitjob/LookWork'),
          meta: {
            active: "/home/submitjob" // 当前首页处于激活状态
          }
        },
        {
          path: "modifywork",
          name: "ModifyWork",
          component: () => import('@/components/submitjob/ModifyWork'),
          meta: {
            active: "/home/submitjob" // 当前首页处于激活状态
          }
        }
      ]
    }
  ],

})

router.beforeEach(async (to, from, next) => {
  console.log(to)
  //当前路由
  // if (to.path == '/home/detail' || to.path == '/home/addcourses') {
  //   Vue.prototype.active = JSON.parse(JSON.stringify(store.state.user_info)).user_auth == 'student_per' ? '/home/studentcourse' : '/home/teachercourse';
  // } else if (to.path.includes("submit") || to.path.includes("lookwork")) {
  //   Vue.prototype.active = "/home/submitjob";
  // } else if (to.path.includes("correct") || to.path.includes("cabbage")) {
  //   Vue.prototype.active = JSON.parse(JSON.stringify(store.state.user_info)).user_auth == 'student_per' ? '/home/correct' : '/home/tcorrent';
  // } else if (to.path.includes("evalute")) {
  //   Vue.prototype.active = "/home/evaluate";
  // } else if (to.path.includes("centre")) {
  //   Vue.prototype.active = JSON.parse(JSON.stringify(store.state.user_info)).user_auth == 'student_per' ? '/home/studentcentre' : '/home/teachercentre';
  // } else {
  //   Vue.prototype.active = to.path;
  // }
  // if (Object.keys(JSON.parse(JSON.stringify(store.state.user_info))).length == 0) {
  //   let res = await axios.post("/login/userInfo", { user_info: localStorage.getItem("userInfo") })
  //   console.log(res);
  //   store.commit("setUser", res.user_info);
  //   store.commit('setActive', to.meta.active)
  // } else {
  //   store.commit('setActive', to.meta.active)
  // }
  console.log(store.state.active);
  console.log(JSON.parse(JSON.stringify(store.state.user_info)));

  // 判断路径是否是登录页
  if (to.path === '/login' || to.path === '/register') {
    // 是登录页，执行下一步
    next()
  }
  else {
    // 不是登录页，判断本地有无 token
    const token = localStorage.getItem('token')
    if (token === null || token === ' ') {
      // token 为空或不存在，跳转到登录页
      next('/login')
    }
    else {
      console.log(Object.keys(JSON.parse(JSON.stringify(store.state.user_info))).length);
      if (Object.keys(JSON.parse(JSON.stringify(store.state.user_info))).length == 0) {
        let res = await axios.post("/login/userInfo", { user_info: localStorage.getItem("userInfo") })
        console.log(res);
        store.commit("setUser", res.user_info);
        // 首页--批改作业页面，根据用户身份跳转页面
        store.commit('setActive', to.meta.active)
        if (to.path === '/home/correct') {
          if (JSON.parse(JSON.stringify(store.state.user_info)).user_auth == 'teacher_per') {
            next('/home/tcorrent')
          }
          else {
            next()
          }
        } else {
          next()
        }
      }
      else {
        // 首页--批改作业页面，根据用户身份跳转页面
        store.commit('setActive', to.meta.active)
        if (to.path === '/home/correct') {
          if (JSON.parse(JSON.stringify(store.state.user_info)).user_auth == 'teacher_per') {
            next('/home/tcorrent')
          }
          else {
            next()
          }
        } else {
          console.log('next');
          next()
        }
      }
      next()
    }
    next()
  }
})

export default router;

